﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Windows.Controls;
using Wpf.Ui;
using Wpf.Ui.Controls;
using WpfMvvmTemplate.WPFUI.Common;

namespace WpfMvvmTemplate.WPFUI.Views.Pages
{
    public partial class SettingsPage
    {
        public SettingsPage()
        {
            InitializeComponent();

            //加载中英文
            ComMethod.LanguageWinDicHelper(GlobalStaticData.LanguageNum, this);
        }

        /// <summary>
        /// 切换中英文语言
        /// </summary>
        private void RadioButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            #region 切换中英文语言

            if (sender is RadioButton clickedButton)
            {
                //读取文件内容
                string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);

                //解析文件内容
                JObject? jObject = JObject.Parse(fileContent);
                JToken? jToken = jObject?["AppSettings"]?["Language"];

                int num = int.Parse(clickedButton.CommandParameter.ToString()!);

                if (int.Parse(jToken!.ToString()) == num) return;

                //修改Language的值
                jToken?.Replace(JToken.FromObject(num));

                //保存
                File.WriteAllText(GlobalStaticData.AppSettingsFilePath, jObject?.ToString());

                //弹框
                if (num == 0)
                {
                    App.Current.Services.GetRequiredService<ISnackbarService>().Show("特别提示：",
                                                            "软件重启后切换语言生效！",
                                                            ControlAppearance.Success,
                                                            new SymbolIcon(SymbolRegular.Fluent24),
                                                            TimeSpan.FromSeconds(3));
                }
                else
                {
                    App.Current.Services.GetRequiredService<ISnackbarService>().Show("Special Note:",
                                                            "Switch languages after software restart!",
                                                            ControlAppearance.Success,
                                                            new SymbolIcon(SymbolRegular.Fluent24),
                                                            TimeSpan.FromSeconds(3));
                }

            }

            #endregion
        }
    }
}
